<script setup lang="ts">
import { getHomeGoodsuessLikeAPI } from '@/services/home'
import { onMounted, ref } from 'vue'
import type { GuessItem } from '@/types/home'
import type { PageParams } from '@/types/global'
// 分页参数
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}

// 猜你喜欢的数据数组
const guessList = ref<GuessItem[]>([])
//结束加载的标记
const finish = ref<boolean>(false)

// 获取猜你喜欢的数据
const getHomeGoodsuessLikeData = async () => {
  if (finish.value) return uni.showToast({ icon: 'none', title: '没有更多数据了' })
  const res = await getHomeGoodsuessLikeAPI(pageParams)
  // guessList.value = res.result.items
  // 数组追加
  guessList.value.push(...res.result.items)
  if (pageParams.page < res.result.pages) {
    // 页码累加
    pageParams.page++
  } else {
    finish.value = true
  }
}
// 重置数据的方法
const resetData = () => {
  pageParams.page = 1
  guessList.value = []
}

//组件挂载完毕调用
onMounted(() => {
  getHomeGoodsuessLikeData()
})

// 暴露获取猜你喜欢数据的方法
defineExpose({
  resetData,
  getmore: getHomeGoodsuessLikeData,
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessList"
      :key="item.id"
      :url="`/pages/goods/index?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name">{{ item.name }}</view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text">{{ finish ? '没有更多数据了' : '正在加载...' }}</view>
</template>

<style lang="scss">
@import '@/components/styles/XTxGuess.scss';
</style>
